/**
* @file MapToHeightmap.cpp
* @author Marcus Antonsson
* @version 2009-12-07
*
* <code>MapToHeightmap</code> is a static class used to generate heightmapimages from
* the map class that is used for the same project
*
**/

#include "MapToHeightmap.h"

namespace Putin {

MapToHeightmap::MapToHeightmap(void)
{
}

MapToHeightmap::~MapToHeightmap(void)
{
}

/**
* Function for saving a heightmap to an image file
* Probably going into its own static class
*
* @param map The map to create a heightmap image from
*/
void MapToHeightmap::generateHeightmapToImage( Map* map )
{
	try 
	{
		Ogre::Image image;
		Ogre::PixelFormat format = Ogre::PF_BYTE_L;
		// Bytes needed for one channel
		const unsigned int BYTES = 1;

		Ogre::uchar* data = new Ogre::uchar[ map->getW() * map->getH() * BYTES ];
		Ogre::uchar* pos = data;

		// Fill data array with info
		for ( int i = 0; i < map->getH(); i++ ) {
			for ( int j = 0; j < map->getW(); j++ ) {
				Ogre::uint val = Ogre::uint( 10*map->getHeight( j, i ) );
				std::memcpy( pos, &val, BYTES );
				pos += BYTES;
			}
		}
		// Write info
		image.loadDynamicImage( data, map->getW(), map->getH(), 1, format, true );
		image.save( "../putinresources/maps/heightmap/putin.png" );
	}
	catch ( Ogre::Exception& e )
	{
		std::cout << "An exception occurred while reading file " 
			<< e.getFile() << "in MapToHeightmap::generateHeightmapToImage()" << std::endl;
	}
}
}// end namespace putin